using System.Collections;
using System.Collections.ObjectModel;
using EzPos.Utility;
using NHibernate.Expression;
using System;

namespace EzPos.DataAccess.Table
{
    public class TableDataAccess : BaseDataAccess
    {
        public virtual IList GetTables()
        {
            var orderList = new Collection<Order> {Order.Asc(Model.Table.Table.ConstTableId)};

            return SelectObjects(typeof (Model.Table.Table), orderList).List();
        }

        public virtual IList GetTables(int id)
        {
            var criterionList = new Collection<ICriterion> {Expression.Eq("TableId", id)};

            var orderList = new Collection<Order> {Order.Asc(Model.Table.Table.ConstTableId)};

            return SelectObjects(typeof (Model.Table.Table), criterionList, orderList).List();
        }

        public virtual IList GetTables(String name)
        {
            var criterionList = new Collection<ICriterion> {Expression.Eq("TableName", name)};

            var orderList = new Collection<Order> {Order.Asc(Model.Table.Table.ConstTableId)};

            return SelectObjects(typeof(Model.Table.Table), criterionList, orderList).List();
        }

        public virtual IList GetTables(IList searchCriteria)
        {
            var criterionList = new Collection<ICriterion>();
            if (searchCriteria != null)
            {
                foreach (string strCriteria in searchCriteria)
                {
                    var delimiterIndex = strCriteria.IndexOf("|");
                    if (delimiterIndex >= 0)
                        criterionList.Add(
                            Expression.Eq(
                                StringHelper.Left(strCriteria, delimiterIndex),
                                StringHelper.Right(strCriteria, strCriteria.Length - delimiterIndex - 1)));
                    else
                        criterionList.Add(Expression.Sql(strCriteria));
                }
            }

            var orderList = new Collection<Order> {Order.Asc(Model.Table.Table.ConstTableName)};

            return SelectObjects(
                typeof(Model.Table.Table), criterionList, orderList).List();
        }

        public virtual void UpdateTable(Model.Table.Table table)
        {
            UpdateObject(table);
        }

        public virtual void InsertTable(Model.Table.Table table)
        {
            InsertObject(table);
        }

        public virtual void DeleteTable(Model.Table.Table table)
        {
            DeleteObject(table);
        }        
    }
}